$.fn.page = function(option){
    var defaults = {
        pageNo: 1,             /*当前页*/
        rows: 20,              /*条数*/
        total: 0,              /*总数*/
        showTabs: 7,           /*显示几个*/
        href: ''               /*链接*/
    };
    option = $.extend({},defaults,option);
    option.mosaicHref = function(href,pageNum){
        // if(href){
        //     return href.substr(0,href.lastIndexOf("/")+1)+pageNum;
        // }
        if(href.indexOf("?")==-1){
            return href+"?page="+pageNum;
        }
        var link = href.substr(0,href.indexOf("?")+1);
        var flag = false;
        var query = href.substr(href.indexOf("?")+1);
        var queryArray=query.split("&"); //各个参数放到数组里
        for(var i=0;i < queryArray.length;i++){
            if(queryArray[i].split("=")[0]=="page"){
                link += "page="+pageNum+"&";
                flag = true;
            }else{
                link += queryArray[i]+"&";
            }
        }
        if(!flag){
            link += "page="+pageNum;
        }else{
            link = link.substr(0,link.length-1);
        }
        return link;
    };
	//page.backfun = option.backfun || function (data) {};
	var maxPage= Math.ceil(option.total/option.rows);

	var pno=option.showTabs;
	//设置第一个显示的页码数  固定当前页码到中间
	var startpage = option.pageNo-(pno%2!=0? (pno-1)/2-1 : pno/2);
	//设置最后一个显示的页码数
	var endpage = option.pageNo+(pno%2!=0? (pno-1)/2-1 : pno/2);
	//如果计算出开始页码小于1 那么开始页码设置为1
	if(startpage<1){
		startpage = 1;
		//如果开始页码为1并且最大页码大于或者等于当前页码 结束页码为页码显示长度
		if(maxPage>=pno) {
            endpage = pno;
        }
		//显示最后一页为页码最大值
		else {
            endpage = maxPage;
        }
	}
	//如果结束页码大于页码最大值
	if(endpage>maxPage){
		//设置结束页码等于页码最大值
		endpage = maxPage;
		//设置最后一组页码的开头页码数
		//如果结束页码减去默认显示的页码长度大于零那么开始页码就是结束页码减去页码长度加1
		if((endpage-pno)>0) {
			startpage = endpage-pno+1;
		}else {
			startpage = 1
		}
	}
	//如果页码长度大于最大页码数那么设置页面长度等于最大页码数
	if(option.pageNo>maxPage){
        option.pageNo=maxPage;
	}

    //创建分页容器
    if(option.total>0){
        var paginationHtml = '<div class="box">';
	    paginationHtml+='<a href="javascript:;" title="共'+maxPage+'页">共'+maxPage+'页</a>';
    }
	//如果页码大于1 显示上一页 并插入到容器
	if(option.pageNo>1){
		paginationHtml+='<a class="page-num" href="'+option.mosaicHref(option.href,1)+'" title="首页">首页</a>';
		paginationHtml+='<a class="page-num" href="'+option.mosaicHref(option.href,option.pageNo-1)+'" title="上一页">上一页</a>';
	}
	//如果页码大于等于3 显示第一页并出现省略号
	if(startpage >=3){
		paginationHtml+='<a class="page-num" href="'+option.mosaicHref(option.href,1)+'" title="1">1</a><span class="ellipsis">...</span>';
	}
	for(var i=startpage;i<=endpage;i++){
		if(i==option.pageNo){
	    	paginationHtml+='<a class="page-num on" href="'+option.mosaicHref(option.href,i)+'" title="'+i+'">'+i+'</a>';
	  	}else{
		    paginationHtml+='<a class="page-num" href="'+option.mosaicHref(option.href,i)+'" title="'+i+'">'+i+'</a>';
	  	}
	}
	if(endpage<=maxPage-2){
	  	paginationHtml+='<span class="ellipsis">...</span><a class="page-num" href="'+option.mosaicHref(option.href,maxPage)+'" title="'+maxPage+'">'+maxPage+'</a>';
	}

	if(option.pageNo<maxPage){
		paginationHtml+='<a  class="page-num" href="'+option.mosaicHref(option.href,option.pageNo+1)+'" title="下一页">下一页</a>';
		paginationHtml+='<a  class="page-num" href="'+option.mosaicHref(option.href,maxPage)+'" title="尾页">尾页</a>';
    }
    if(option.total>0){
        paginationHtml+='<span class="jump_page">第 <input type="text" data-max="'+maxPage+'" data-num="'+option.pageNo+'" id="RG_Jump_Page" class="text-b" value="'+option.pageNo+'"> 页</span>';
        paginationHtml+='<a href="'+option.mosaicHref(option.href,option.pageNo)+'" class="jump_qd" id="RG_Jump_Qd" title="确定">确定</a>';
        paginationHtml+='</div>';
    }
    $(this).html(paginationHtml);
	//return paginationHtml;
}
$(function(){
    console.log('分页调用位置')
    if($('#pagination')){
        var xPage = $('#pagination').attr("data-page");
        $('#pagination').page({
            pageNo: xPage.split(",")[1],             /*当前页*/
            rows: 25,              /*条数*/
            total: xPage.split(",")[0],          /*总数*/
            showTabs: 7,           /*显示几个*/
            href: location.href    /*链接*/
        });
    }

})

$(function(){
    var RGJumpPage = $("#RG_Jump_Page");
    var RGJumpQd = $("#RG_Jump_Qd");
    RGJumpPage.focus(function(){
        
    });
    function getHref(href,pageNum){
        // if(href){
        //     return href.substr(0,href.lastIndexOf("/")+1)+pageNum;
        // }
        if(href.indexOf("?")==-1){
            return href+"?page="+pageNum;
        }
        var link = href.substr(0,href.indexOf("?")+1);
        var flag = false;
        var query = href.substr(href.indexOf("?")+1);
        var queryArray=query.split("&"); //各个参数放到数组里
        for(var i=0;i < queryArray.length;i++){
            if(queryArray[i].split("=")[0]=="page"){
                link += "page="+pageNum+"&";
                flag = true;
            }else{
                link += queryArray[i]+"&";
            }
        }
        if(!flag){
            link += "page="+pageNum;
        }else{
            link = link.substr(0,link.length-1);
        }
        return link;
    }
    RGJumpPage.keydown(function(e){
        if((e.which>=48&&e.which<=57)||(e.which>=96&&e.which<=105)){
            if(parseInt($(this).val()+''+getNum (e.which))>=parseInt($(this).attr('data-max'))){
                $(this).val($(this).attr('data-max'));
                var href = RGJumpQd.attr('href');
                RGJumpQd.attr('href',getHref(href,$(this).attr('data-max')));
                return false;
            }
            if(parseInt($(this).val()+''+getNum (e.which))<=0){
                $(this).val(1);
                var href = RGJumpQd.attr('href');
                RGJumpQd.attr('href',getHref(href,1));
                return false;
            }
            if(parseInt($(this).val()+''+getNum (e.which))>0&&parseInt($(this).val()+''+getNum (e.which))<parseInt($(this).attr('data-max'))){
                $(this).val(parseInt($(this).val()+' '+getNum (e.which)));
                var href = RGJumpQd.attr('href');

                RGJumpQd.attr('href',getHref(href,parseInt($(this).val()+''+getNum (e.which))));
            }
        }else if(e.which==8||e.which==13||e.which==37||e.which==39){
            if(e.which==8){
                var value = $(this).val();
                if(value.length==1){
                    if(value<1){
                        $(this).val(1);
                    }
                    return false;
                }
                $(this).val(parseInt(value));
                var href = RGJumpQd.attr('href');
                RGJumpQd.attr('href',getHref(href,parseInt(value/10)));
            }
            if(e.which==13){
                location.href = RGJumpQd.attr('href');
            }
        }else{
            return false 
        }
    });
    function getNum (code){
        switch(parseInt(code)){
            case 48:
                return 0;
                break;
            case 96:
                return 0;
                break;
            case 49:
                return 1;
                break;
            case 97:
                return 1;
                break;
            case 50:
                return 2;
                break;
            case 98:
                return 2;
                break;
            case 51:
                return 3;
                break;
            case 99:
                return 3;
                break;
            case 52:
                return 4;
                break;
            case 100:
                return 4;
                break;
            case 53:
                return 5;
                break;
            case 101:
                return 5;
                break;
            case 54:
                return 6;
                break;
            case 102:
                return 6;
                break;
            case 55:
                return 7;
                break;
            case 103:
                return 7;
                break;
            case 56:
                return 8;
                break;
            case 104:
                return 8;
                break;
            case 57:
                return 9;
                break;
            case 105:
                return 9;
                break;
            default:
                return '';
                break;
        }
    }
});